<?php 
define("CCTRL", "order");
define("CCTRLNAME", "order");
define("TOKEN", "5cf8b6c68201bc1ea74e972d84b90c0e");
session_start();

class order extends CI_Controller  {

        /**
        * Index Page for this controller.
        *
        * Maps to the following URL
        * 		http://example.com/index.php/welcome
        *	- or -  
        * 		http://example.com/index.php/welcome/index
        *	- or -
        * Since this controller is set as the default controller in 
        * config/routes.php, it's displayed at http://example.com/
        *
        * So any other public methods not prefixed with an underscore will
        * map to /index.php/welcome/<method_name>
        * @see http://codeigniter.com/user_guide/general/urls.html
        */
       private $_configs = null;
       function __construct()
        {
            parent::__construct();
            $this->load->helper('url');
            $this->load->library('smarty3');
            $this->load->model('colmod/order_colmod');
            $this->load->model('APanel/order_model');
            $this->load->model('APanel/product_model');
            $this->load->model('APanel/my_model');
            include APPPATH . 'libraries/defu.php';
            if(!isset($_SESSION["ADP"][CCTRL]["display"]))$_SESSION["ADP"][CCTRL]["display"]=0;
            if(!isset($_SESSION["ADP"][CCTRL]["pageSize"]))$_SESSION["ADP"][CCTRL]["pageSize"]=100;
            if(!isset($_SESSION["ADP"][CCTRL]["status"]))$_SESSION["ADP"][CCTRL]["status"]=0;
            $this->colModel=new order_colmod();
            $this->smarty=new CI_Smarty3();$this->smarty->error_reporting = E_ALL & ~E_NOTICE;
            $this->order_model=new order_model();
            $this->product_model=new product_model();
            //$this->j_model=new my_model("dtb_orders");
            
        }
        
        public function index()
	{
            //unset($_SESSION["ADP"][CCTRL."-JQX"]["colModel"]);
            if(!checkAuthority(CCTRLNAME)){
                $this->smarty
                        ->assign('_SESSION', $_SESSION)
                        ->display("sys/04_access_denied");
                return;
            }
            if(isset($_SESSION["ADP"][CCTRL."-JQX"]["colModel"]))
                $colModel=$_SESSION["ADP"][CCTRL."-JQX"]["colModel"];
            else{
                $colModel=$this->colModel->get();
                $_SESSION["ADP"][CCTRL."-JQX"]["colModel"]=$colModel;
            }
            $Data["colModel"]=$colModel;
            $Data["display"]=$_SESSION["ADP"][CCTRL]["display"];
            $Data["pageSize"]=$_SESSION["ADP"][CCTRL]["pageSize"];
            $Data["aStatus"]=$_SESSION["ADP"][CCTRL]["status"];
            $this->smarty
                    ->assign('_SESSION', $_SESSION)
                    ->assign('Data', $Data)
                    ->display("APanel/".CCTRL."/".CCTRL);
	}
        function Edit($ID=""){
            $Data=null;
            if($ID!=""){
                $oOrder=$this->order_model->get($ID);
                if(isset($oOrder[0]))$Data["OBJ"]=$oOrder[0];
            }
            $this->smarty
                    ->assign('_SESSION', $_SESSION)
                    ->assign('Data', $Data)
                    ->display("APanel/".CCTRL."/02_edit");
        }
        function changestatus(){
            $Status=$this->input->post("Status");
            $ID=$this->input->post("ID");
            $tmp=$this->order_model->get($ID);
            if(isset($tmp[0])){
                $oOrder=$tmp[0];
                $params=array(
                    "Status"=>$Status
                );
                if($this->order_model->update($ID, $params)){
                    if($oOrder["Status"]=="Finished" && $Status!="Finished"){
                        //restore
                        $this->product_model->updateSold($oOrder["ProductID"],-1*$oOrder["Amount"]);
                    }elseif($oOrder["Status"]!="Finished" && $Status=="Finished"){
                        //update
                        $this->product_model->updateSold($oOrder["ProductID"],$oOrder["Amount"]);
                    }
                    $code=1;
                    $msg="[Fail] Thành công.";
                }else{
                    $code=-1;
                    $msg="[Fail] Thất bại, vui lòng thử lại.";
                }
            }else{
                $code=-400;
                $msg="[Fail] Thất bại, Không tìm thấy đơn hàng.";
            }
            echo json_encode(array("code"=>$code,"msg"=>$msg));
        }
        function jqgrid_data(){
            $colModel=$this->colModel->get();
            $orders=array();
            $result['total_rows']=0;
            //if($this->checkauthority()>=0){
                $result=$this->order_model->binding();
                $rows=$result['rows'];
                // get data and store in a json array
                foreach ($rows as $row) {
                    $cel=array();
                    foreach ($colModel as $col){
                        $f=$col["datafield"];
                        $cel[$f]=$row->$f;
                    }
                    $orders[] = $cel;
                }
            
            $data[] = array(
                'TotalRows' => $result['total_rows'],
                'Rows' => $orders
            );
            echo json_encode($data);
        }
        function changeFilterbyStatus(){
            $value=(int)$this->input->post("value",true);
            $_SESSION["ADP"][CCTRL]["status"]=$value;
            echo json_encode(array("code"=>1,"msg"=>""));
        }
        
        
        
       
        
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */